package bomber.model;


/** class manage box array */
public class GameBoxArray {
	private GameBox gameBox[][];
	private int size;

	public GameBoxArray(int size) {
		this.gameBox = new GameBox[size][size];
		this.size = size;

		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				this.gameBox[i][j] = new GameBox();
			}
		}
	}

	public boolean hasBomb(int i, int j) {
		if (this.size > i && this.size > j) {
			return (this.gameBox[i][j].getType() == GameBox.BOMB);
		}
		return false;
	}

	public boolean setBomb(int i, int j) {
		if (this.size > i && this.size > j) {
			this.gameBox[i][j].setType(GameBox.BOMB);
			return true;
		}
		return false;
	}

	public void makeInfo(int i, int j, int rock) {
		int number = 0;
		int ii = 0;
		int jj = 0;

		for (int m = 0; m < 3; m++) {
			for (int n = 0; n < 3; n++) {
				ii = i + m - 1;
				jj = j + n - 1;
				
				if (ii >= 0 && ii < this.size && jj >= 0 && jj < this.size) {
					if (this.hasBomb(ii, jj)) number++;
				}
			}
		}
		if (number > 0) {
			if (number < rock) {
				this.gameBox[i][j].setType(GameBox.NUMB);
				this.gameBox[i][j].setInfo(number);
			}
			else this.gameBox[i][j].setType(GameBox.ROCK);
		}
	}

	public GameBox getOne(int i, int j) {
		if (this.size > i && this.size > j)  {
			return this.gameBox[i][j];
		}
		return null;
	}

	public GameBox[][] getAll() {
		return this.gameBox;
	}
}
